// vim:ft=javascript:tabstop=2:shiftwidth=2:softtabstop=2:expandtab

/*Plugin: add prefix and suffix when sending. */
k_plugins.prefix={
  name:'Prefix & Suffix',
  ui:function(){
    return 'Prefix:<input type="text" style="width:100px;" id="prefix" value="'+getCookie('msg_prefix')+'" class="border1" /> Suffix:<input type="text" style="width:100px;" id="suffix" value="'+getCookie('msg_suffix')+'" class="border1" />';
  },
  init:function(){
    $('#prefix').blur(function(){
        setCookie('msg_prefix',$('#prefix').val(),1000);
      });
    $('#suffix').blur(function(){
        setCookie('msg_suffix',$('#suffix').val(),1000);
      });
  },
  beforesend:function(msg){
    return $.trim($.trim(getCookie('msg_prefix')+' '+msg)+getCookie('msg_suffix'));
  }
};

/*Plugin: Switch off Geo-information*/
k_plugins.geoinformation={
  name:'Geo',
  ui:function(){
    return '<input type="checkbox" id="checkboxgeo" value="stop" '+(getCookie('stopgeoinfo') == 'stop'?'checked':'')+' />Prevent kwestion from sending Geo-information.';
  },
  init:function(){
    $('#checkboxgeo').click(function(){
        setCookie('stopgeoinfo',($('#checkboxgeo:checked').size()>0?"stop":''),1000);
      });
  }
};

/*Plugin: filter.*/

k_plugins.filter_content = {
  name: 'Filter',
  ui: function() {
    return '<div>Ignore tweets containing the following strings. Each string a line.</div><textarea id="filter_content" class="border1"></textarea><div>Ignore tweets sent by the following IDs. Each string a line.</div><textarea id="filter_content_id" class="border1"></textarea>';
  },
  init: function() {
    $('#filter_content').val(getCookie('filter_content'));
    $('#filter_content').blur(function() {
        setCookie('filter_content', $('#filter_content').val(), 1000);
      });
    $('#filter_content_id').val(getCookie('filter_content_id'));
    $('#filter_content_id').blur(function() {
        setCookie('filter_content_id', $('#filter_content_id').val(), 1000);
      });
  },
  filter: function(msg, type) {
    var xxx = getCookie('filter_content').split("\n");
    if (!xxx.length) return true;
    for(var i = xxx.length - 1; i >=0; i--){
      var ttt = $.trim(xxx[i]);
      if(ttt.length && msg.text && msg.text.indexOf(ttt)>=0) return false;
    }
    xxx = getCookie('filter_content_id').split("\n");
    if (!xxx.length) return true;
    for(var i = xxx.length - 1; i >=0; i--){
      var ttt = $.trim(xxx[i]).toLowerCase();
      if(ttt.length && ((msg.user 
            && msg.user.screen_name 
            && msg.user.screen_name.toLowerCase() == ttt)
          || (msg.from_user && msg.from_user.toLowerCase() == ttt))) return false;
    }
    return true;
  }
};

/*Plugin: images' thumbs. */
k_plugins.imgpreview = {
  name:'Preview images',
  ui: function(){
    return 'Working...';
  },
  init: function(){

  },
  filter: function(msg,type){
    if(msg.processedtext){
      msg.processedtext = ' ' + msg.processedtext
      .replace(/>http:\/\/instagr.am\/p\/([\d\w]+?)\/</ig,'>http://instagr.am/p/$1/<img src="http://instagr.am/p/$1/media/?size=t" /><');
      //.replace(/>http:\/\/img.ly\/(.*?)</ig,'>http://img.ly/$1<img src="http://img.ly/show/medium/$1" /><')
      //.replace(/>http:\/\/picplz.com\/([\d\w]+?)</ig, '>http://picplz.com/$1 <img src="http://picplz.com/$1/thumb/200" /><')
      //.replace(/>http:\/\/picplz.com\/.*?\/pic\/([\d\w]+?)\/</ig, '>http://picplz.com/$1 <img src="http://picplz.com/$1/thumb/200" /><');
    }
    return true;
  }
};

k_plugins.showretweet = {
  name:'Format retweets',
  ui: function(){
    return 'Working...';
  },
  init: function(){

  },
  filter: function(msg, type){
    if(msg.retweeted_status){
      msg.processedtext += '<div class="retweet_status">This is an official retweet'+(msg.retweet_count?(' <span>('+msg.retweet_count+(msg.retweet_count<2?' time':' times')+')'):'')+'</span>.</div>';
    }
    return true;
  }
};

k_plugins.closenotify = {
  name:'Close notification',
  ui: function(){
    return '<input type="checkbox" id="closenotification" '+((getCookie('notify')=='no')?'checked':'')+' />Check to close notification.';
  },
  init: function(){
    $('#closenotification').click(function(){
        setCookie('notify',(getCookie('notify')!='no')?'no':'',1000);
      });
  }
};

